if (Meteor.isServer) { 
  //商品
  Meteor.publish('products', function () {
    if(this.userId){
      return Products.find({'company_id':Meteor.users.findOne(this.userId).company_id});
    }
  });
}

Meteor.methods({
  /**
   * 添加商品
   */
  addProduct:function(newProduct){
    checkLogin(this.userId);
    var user = Meteor.users.findOne(this.userId,{_id:0,'company_id':1,'profile.pmsn_group':1});
    if (!newProduct.catalog) {
      throw new Meteor.Error(10050,'分类名称不能为空。');
    }
    newProduct.company_id = user.company_id;
    try {
      newProductId = Products.insert(newProduct);
    } catch (e) {
      throw e;
    }
    return newProductId;
  },
  deleteProduct:function(productId){
    checkLogin(this.userId);
    var theProduct = Products.findOne(productId,{'company_id':1});
    checkCompany(this.userId,theProduct.company_id);
    try {
      pid = Products.remove(productId);
    } catch (e) {
      throw e;
    }
    return pid;
  },
  updateProduct:function(theProduct){
    checkLogin(this.userId);
    var product = Products.findOne(theProduct._id,{'company_id':1});
    checkCompany(this.userId,product.company_id);
    try {
      theProductToUpdateId = Products.update({'_id':theProduct._id},
        { $set: { 'catalog':theProduct.catalog,
                  'barcode':theProduct.barcode,
                  'properties':theProduct.properties,
                  'cost_price_defualt':theProduct.cost_price_defualt,
                  'sale_price':theProduct.sale_price,
                  'min_sale_price':theProduct.min_sale_price,
                  'thumb':theProduct.thumb}
      });
    } catch (e) {
      throw e;
    }
  }
});

// 商品
if (Products.find().count() === 0) {
  Products.insert({
    _id:'1',
    catalog:'手机',
    barcode:'9652524125412',
    properties:{
      'brand':{name:'品牌',value:'苹果',logo:'apple'},
      'model':{name:'型号',value:'iphone 6 plus(A1530)'},
      'color':{name:'颜色',value:'白色'},
      'netType':{name:'网络',value:'移动4G'},
      'version':{name:'版本',value:'国行'},
      'memoInside':{name:'机身内存',value:'64G'}
    },
    //以下为商家自定义
    thumb:'img/1.jpg',
    pics:[],
    single_barcode:'6920002010232',
    cost_price_default:5188,
    sale_price:6200,
    min_sale_price:5500,
    company_id:'1'
  });

  Products.insert({
    _id:'2',
    catalog:'手机',
    barcode:'2524123456125',
    properties:{
      'brand':{name:'品牌',value:'小米',logo:'xiaomi'},
      'model':{name:'型号',value:'小米5 高配版'},
      'color':{name:'颜色',value:'白色'},
      'netType':{name:'网络',value:'移动4G'},
      'version':{name:'版本',value:'国行'},
      'memoInside':{name:'机身内存',value:'128G'}
    },
    //以下为商家自定义
    thumb:'img/3.jpg',
    pics:[],
    single_barcode:'6920002014367',
    cost_price_default:2000,
    sale_price:2100,
    min_sale_price:1800,
    company_id:'1'
  });

  Products.insert({
    _id:'3',
    catalog:'手机',
    barcode:'2524123456220',
    properties:{
      'brand':{name:'品牌',value:'魅族',logo:'meizu'},
      'model':{name:'型号',value:'魅族 MX5(M1530)'},
      'color':{name:'颜色',value:'白色'},
      'netType':{name:'网络',value:'移动3G'},
      'version':{name:'版本',value:'国行'},
      'memoInside':{name:'机身内存',value:'32G'}
    },
    //以下为商家自定义
    thumb:'img/2.jpg',
    pics:[],
    single_barcode:'6920002010452',
    cost_price_default:1500,
    sale_price:2700,
    min_sale_price:2100,
    company_id:'1'
  });

  Products.insert({
    _id:'4',
    catalog:'手机',
    barcode:'2524123456125',
    properties:{
      'brand':{name:'品牌',value:'华为',logo:'huawei'},
      'model':{name:'型号',value:'荣耀8(R1530)'},
      'color':{name:'颜色',value:'白色'},
      'netType':{name:'网络',value:'移动4G'},
      'version':{name:'版本',value:'国行'},
      'memoInside':{name:'机身内存',value:'8G'},
      'memoCard':{name:'内存卡',value:'64G'}
    },
    thumb:'img/4.jpg',
    pics:[],
    single_barcode:'1011001013141',
    cost_price_default:2800,
    sale_price:3600,
    min_sale_price:3100,
    company_id:'1'
  });
}
